package com.rustic.server.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rustic.server.dao.RestaurantDao;
import com.rustic.server.po.Restaurant;
import com.rustic.server.query.RestaurantAdminListQuery;
import com.rustic.server.service.RestaurantService;
import com.rustic.server.vo.list.RestaurantAdminListVO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Restaurant对应的服务类实现
 *
 * @author auto
 */

@Service
public class RestaurantServiceImpl extends ServiceImpl<RestaurantDao, Restaurant> implements RestaurantService {

    @Override
    public List<Restaurant> listByIdIn(List<Integer> idList) {
        if (CollUtil.isEmpty(idList)) {
            return new ArrayList<>();
        }
        return this.listByIds(idList);
    }

    @Override
    public Map<Integer, Restaurant> mapByIdIn(List<Integer> idList) {
        List<Restaurant> restaurantList = this.listByIdIn(idList);
        return restaurantList.stream().collect(Collectors.toMap(Restaurant::getId, b -> b));
    }

    @Override
    public LambdaQueryWrapper<Restaurant> toAdminListWrapper(RestaurantAdminListQuery query) {
        LambdaQueryWrapper<Restaurant> queryWrapper = new LambdaQueryWrapper<>();
        //FIXME 根据需要修改搜索条件
        queryWrapper.eq(CharSequenceUtil.isNotEmpty(query.getName()), Restaurant::getName, query.getName());
        queryWrapper.eq(CharSequenceUtil.isNotEmpty(query.getAddress()), Restaurant::getAddress, query.getAddress());
        queryWrapper.eq(CharSequenceUtil.isNotEmpty(query.getDetailedAddress()), Restaurant::getDetailedAddress, query.getDetailedAddress());
        queryWrapper.gt(query.getStartCreateTime() != null, Restaurant::getCreateTime, query.getStartCreateTime());
        queryWrapper.lt(query.getEndCreateTime() != null, Restaurant::getCreateTime, query.getEndCreateTime());
        return queryWrapper;
    }

    @Override
    public List<RestaurantAdminListVO> toAdminListVo(List<Restaurant> restaurantList) {
        List<RestaurantAdminListVO> results = new ArrayList<>();
        for (Restaurant restaurant : restaurantList) {
            RestaurantAdminListVO restaurantAdminListVO = new RestaurantAdminListVO(restaurant);
            results.add(restaurantAdminListVO);
        }
        return results;
    }

}
